package com.example.qxfw.qxzs.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.zy.ZhishuSet;
import com.example.qxfw.qxzs.mapper.ZhishuSetMapper;
import com.example.qxfw.qxzs.service.ZhishuSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZhishuSetServiceImpl implements ZhishuSetService {

    @Autowired
    private ZhishuSetMapper zhishuSetMapper;

    @Override
    public Map<String, List<ZhishuSet>> listSelected() {
        Map<String,List<ZhishuSet>> selectMap = new HashMap<>();

        List<ZhishuSet> zhishuSetsList = zhishuSetMapper.selectList(Wrappers.<ZhishuSet>lambdaQuery()
                .eq(ZhishuSet::getIsselected, 1)
                .orderByAsc(ZhishuSet::getIndexid));
        for (ZhishuSet zhishuSet : zhishuSetsList) {
            if (selectMap.containsKey(zhishuSet.getIndextypename())){
                List<ZhishuSet> zhishuSets = selectMap.get(zhishuSet.getIndextypename());
                zhishuSets.add(zhishuSet);
                selectMap.put(zhishuSet.getIndextypename(),zhishuSets);
            } else {
                List<ZhishuSet> zhishuSets = new ArrayList<>();
                zhishuSets.add(zhishuSet);
                selectMap.put(zhishuSet.getIndextypename(),zhishuSets);
            }
        }

        return selectMap;
    }

    @Override
    public Map<String, List<ZhishuSet>> listUnSelected() {
        Map<String,List<ZhishuSet>> unSelectMap = new HashMap<>();

        List<ZhishuSet> zhishuSetsList = zhishuSetMapper.selectList(Wrappers.<ZhishuSet>lambdaQuery()
                .eq(ZhishuSet::getIsselected, 0)
                .orderByAsc(ZhishuSet::getIndexid));
        for (ZhishuSet zhishuSet : zhishuSetsList) {
            if (unSelectMap.containsKey(zhishuSet.getIndextypename())){
                List<ZhishuSet> zhishuSets = unSelectMap.get(zhishuSet.getIndextypename());
                zhishuSets.add(zhishuSet);
                unSelectMap.put(zhishuSet.getIndextypename(),zhishuSets);
            } else {
                List<ZhishuSet> zhishuSets = new ArrayList<>();
                zhishuSets.add(zhishuSet);
                unSelectMap.put(zhishuSet.getIndextypename(),zhishuSets);
            }
        }

        return unSelectMap;
    }


    @Override
    public List<ZhishuSet> getList2() {
        List<ZhishuSet> zhishuSets = zhishuSetMapper.selectList(null);
        return zhishuSets;
    }

    @Override
    public Integer updateById(ZhishuSet zhishuSet) {
        return zhishuSetMapper.updateById(zhishuSet);
    }

    @Override
    public List<Integer> getSelectId() {
        return zhishuSetMapper.getSelectId();
    }
}
